/**
 * 功能描述
 */

define(function (require, exports, module) {
	var Dialog = require("dialog");
	var aw = require("ajaxwrapper");
	var fetchProperties = require("../constant/fetchProperties");
	
    module.exports = {
    	checkout:function(data,widget){
    		var form = widget.get("form");
    		if(data.checkInImplement.dataSource.key == "GroupReserve"){
				widget.memDataSource = true;
			}else{
				widget.memDataSource = false;
			}
    		if(data.roomRegister.roomStatus.key != "finish" && !data.roomRegister.waterElectric){
    			Dialog.alert({
            		title:"提示",
            		content:"还没有进行退住抄表，不能进行退住！"
            	});
				return false;
    		}
    		widget.show([".J-form",".J-topform", ".J-editgrid"]).hide(".J-grid");
			widget.get("subnav").show(["cancel"]).hide(["servicePoint", "checkOutStatus", "travelDate","buildings"]);
			widget.get("topform").setData(data);
			widget.get("topform").setValue("room", data.roomRegister.room.orgBuilding.name + "-" + data.roomRegister.room.code);
			form.setData(data);
			if(data.roomRegister.roomStatus.key == "finish"){
				var useWater = data.roomRegister.checkOutWaterMeter - data.roomRegister.waterMeter;
				var useHotWater = data.roomRegister.checkOutHotWaterMeter - data.roomRegister.hotWaterMeter;
				var useElectricMeter = data.roomRegister.checkOutElectricMeter - data.roomRegister.electricMeter;
				form.setValue("useWater",useWater);
				form.setValue("useHotWater",useHotWater);
				form.setValue("useElectricMeter",useElectricMeter);
				form.setValue("roomRegister.waterMeter",data.roomRegister.checkOutWaterMeter);
				form.setValue("roomRegister.hotWaterMeter",data.roomRegister.checkOutHotWaterMeter);
				form.setValue("roomRegister.electricMeter",data.roomRegister.checkOutElectricMeter);
			}
			
			var pkRoom = data.roomRegister.room.pkRoom;
			
			if(data.roomRegister.waterElectric){
				form.setValue("roomRegister.waterElectric","是");
			}else{
				form.setValue("roomRegister.waterElectric","否");
			}
			
			var baseprice = widget.get("baseprice");
			form.setValue("waterPrice",baseprice.water);
			form.setValue("hotWaterPrice",baseprice.hotWater);
			form.setValue("electricPrice",baseprice.electric);
			
			var roomPersonalInfos = data.checkInImplement.roomPersonalInfos;
			
			var tRoomPersonalInfos = [];
			for(var j=0;j<roomPersonalInfos.length;j++){
				if(roomPersonalInfos[j].roomRegister.room && pkRoom == roomPersonalInfos[j].roomRegister.room.pkRoom){
					tRoomPersonalInfos.push(roomPersonalInfos[j]);
				}
			}
			
			widget.get("topform").setValue('countMember',tRoomPersonalInfos.length);
			var description = data.checkInImplement.nonMemGroupReserve ? 
					data.checkInImplement.nonMemGroupReserve.nonMemberGroup.description : 
						data.checkInImplement.groupReserve.planItemGroup.description;
			widget.get("topform").setValue('description',description);
			
			var pkRoomPerList = "";
			for(var i=0;i<tRoomPersonalInfos.length;i++){
				if(i == 0){
					pkRoomPerList = tRoomPersonalInfos[i].pkRoomPersonalInfo;
				}else{
					pkRoomPerList += "," + tRoomPersonalInfos[i].pkRoomPersonalInfo;
				}
			}
			widget.set("pkRoomPerList", pkRoomPerList);
			widget.handler.gridHandler.roomPerGridRefresh(widget);
			form.setDisabled("useWater",true);
			form.setDisabled("useHotWater",true);
			form.setDisabled("useElectricMeter",true);
    	},
    	roomPerGridRefresh: function(widget){
    		var params = {};
    		if(widget.get("pkRoomPerList")){
    			params={
    				pkRoomPersonalInfoIn:widget.get("pkRoomPerList"),
    	            "fetchProperties": fetchProperties.roomPerGrid	
    			};
    		}else{
    			params={
    	            "fetchProperties": fetchProperties.roomPerGrid	
    			};
    		}
    		aw.ajax({
    			url: "api/roompersonalInfo/query",
    	        data: params,
    	        dataType: "json",
    	        success: function(data){
    	        	widget.get("roomPerGrid").setData(data);
    	        	var roomConsumption = 0;//房内消费
    	        	var damages = 0;//赔偿费
    	        	for(var i=0; i < data.length; i++){
        				roomConsumption += data[i].roomConsumption;
        				damages += data[i].damagesConsumption;
    	        	}
    	        	var form = widget.get("topform");
    	        	form.setValue("roomConsumption",parseFloat(roomConsumption).toFixed(2));
    	        	form.setValue("damages",parseFloat(damages).toFixed(2));
    	        }
    		})
    	},
    	consumption: function(data,widget,edit){
    		if(edit){
    			widget.show([".J-consumpeditgrid"]);
    			widget.get("subnav").show("save");
    		}else{
    			widget.show([".J-consumpdetailgrid"]);
    			widget.get("subnav").hide("save");
    		}
    		widget.hide([".J-form",".J-topform", ".J-editgrid"]);
			widget.get("subnav").show(["return"]).hide(["cancel"]);
			widget.get("subnav").setTitle("录入消费信息");
			
			var damagesConsumpData = [],roomConsumpEditData = [];
			
			var consumpData = data.roomPerConsumptions;
			for(var i = 0; i < consumpData.length; i++){
				if(consumpData[i].consumptionType.key == "RoomConsumption"){
					roomConsumpEditData.push(consumpData[i]);
				}else{
					damagesConsumpData.push(consumpData[i]);
				}
			}
			widget.set("pkRoomPersonalInfo",data.pkRoomPersonalInfo);
			widget.get("roomConsumpEdit").setData(roomConsumpEditData);//房内消费
			widget.get("damagesConsumpEdit").setData(damagesConsumpData);//赔偿消费
			
			widget.get("roomConsumpDetailGrid").setData(roomConsumpEditData);
			widget.get("damagesConsumpDetailGrid").setData(damagesConsumpData);
    	},
    	
    	confirmData: function(widget, pkRoomPersonalInfo){
    		if(widget.get("form").valid()){
    			var topformData = widget.get("topform").getData();
    			var formData = widget.get("form").getData();
    			var editgridData = widget.get("roomPerGrid").getData();
    			
    			var checkOutNum = 0;
    			var list = [];
    			var isAlert = false;
    			var allOut = false;
    			for(var i=0;i<editgridData.length;i++){

    				var pkSignUpRegister,checkOutDate,checkOut=false;
    				
					if(editgridData[i].pkRoomPersonalInfo == pkRoomPersonalInfo){
						if(widget.memDataSource){
							editgridData[i].signUpRegister.checkOut = true;
							editgridData[i].signUpRegister.checkOutDate = moment().format("x");
							pkSignUpRegister = editgridData[i].signUpRegister.pkSignUpRegister;
							checkOutDate = editgridData[i].signUpRegister.checkOutDate;
						}else{
							editgridData[i].nonMemberRegister.signUpRegister.checkOut = true;
							if(editgridData[i].nonMemberRegister.signUpRegister.checkOutDate == null){
								editgridData[i].nonMemberRegister.signUpRegister.checkOutDate = moment().format("x");
							}
							pkSignUpRegister = editgridData[i].nonMemberRegister.signUpRegister.pkSignUpRegister;
							checkOutDate = editgridData[i].nonMemberRegister.signUpRegister.checkOutDate;
						}
					}
					if(widget.memDataSource){
						checkOut = editgridData[i].signUpRegister.checkOut;
					}else{
						checkOut = editgridData[i].nonMemberRegister.signUpRegister.checkOut;
					}

    				if(checkOut){
    					checkOutNum++;
    				}
    				
    				list.push({
    					"pkRoomPersonalInfo":editgridData[i].pkRoomPersonalInfo,
    					"signUpRegister.pkSignUpRegister":pkSignUpRegister,
    					"signUpRegister.checkOutDate":checkOutDate,
    					"signUpRegister.checkOut":checkOut
    				});
    			}
    			
    			if(checkOutNum == editgridData.length){
    				allOut = true;
    			}
    			
    			var data = {
    				"pkCheckIn":topformData.pkCheckIn,
    				"checkOut":allOut,
    				"checkOutWaterMeter":formData["roomRegister.waterMeter"] == "" ? 0 : parseFloat(formData["roomRegister.waterMeter"]),
					"checkOutHotWaterMeter":formData["roomRegister.hotWaterMeter"] == "" ? 0 : parseFloat(formData["roomRegister.hotWaterMeter"]),
    				"checkOutElectricMeter":formData["roomRegister.electricMeter"] == "" ? 0 : parseFloat(formData["roomRegister.electricMeter"]),
    				"list":list,
    				"memDataSource": widget.memDataSource
    			};
    			
    			data = aw.customParam(data);
        		
        		aw.saveOrUpdate("api/checkin/checkout",data,function(data){
        			Dialog.alert({
        				content:"保存成功"
        			});
        			widget.show([".J-form",".J-topform", ".J-editgrid"]).hide(".J-consumpeditgrid");
					widget.get("subnav").hide(["save","return"]).show("cancel").hide(["servicePoint", "checkOutStatus", "travelDate","buildings"]);
					widget.get("subnav").setTitle("退房查房");
					widget.handler.gridHandler.roomPerGridRefresh(widget);
					widget.get("grid").refresh();
    			});
    		}
    	}
    };
});
